package com.javaenzk.wfx.manager.configs;


import com.javaenzk.wfx.manager.po.Wxb_Customer;

import com.javaenzk.wfx.manager.service.WxbGoodService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;


@Component
public class AccountRelam  extends AuthorizingRealm {

    @Resource
    private WxbGoodService wxbGoodService;

    /**
     * 权限
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }

    /**
     * 登录
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        String username = usernamePasswordToken.getUsername();
        final char[] password1 = usernamePasswordToken.getPassword();
        String password = new String(password1);

        final Wxb_Customer wxbCustomer = wxbGoodService.queryBynameAndPwd(username, password);

        SecurityUtils.getSubject().getSession().setAttribute("user",wxbCustomer);

        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(username,password,"AccountRealm");

        return simpleAuthenticationInfo;
    }
}
